//
//  DatePickerView.m
//  CFfuturestalent
//
//  Created by 子yu 刘 on 2018/5/1.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "DatePickerView.h"

@interface DatePickerView ()
@property (nonatomic,strong)UIDatePicker * datePicker;

@end

@implementation DatePickerView

+(DatePickerView *)sharedInstance{
    static DatePickerView * pickerView;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        pickerView = [[DatePickerView alloc] init];
        
    });
    return pickerView;
}

+ (void)showToView:(UIView *)superView selectedData:(dateBlock)block{
    [[DatePickerView sharedInstance] initViewWithOnView:superView selectedData:block];
}
- (void)initViewWithOnView:(UIView *)superView selectedData:(dateBlock)block {
    _block = block;
    self.frame = superView.bounds;
    [superView addSubview:self];
}

-(instancetype)init{
    if (self = [super init]) {
        [self initView];
    }
    return self;
}

- (void)initView{
    self.backgroundColor = [UIColor clearColor];
    UIView * bgView = [[UIView alloc] init];
    bgView.backgroundColor = DEF_COLOR_HOMETABBGCOLOR;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_offset(235);
    }];
    UIView * btnView = [[UIView alloc] init];
    btnView.backgroundColor = [UIColor clearColor];
    [bgView addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_offset(60);
    }];
    UIView * line = [[UIView alloc] init];
    line.backgroundColor = DEF_COLOR_LINE;
    [btnView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    UIButton * cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:18.0];
    [btnView addSubview:cancleBtn];
    [cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(btnView.mas_centerY);
        make.left.mas_equalTo(30);
    }];
    
    UIButton * confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
     [confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:18.0];
    [btnView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(btnView.mas_centerY);
        make.right.mas_equalTo(-30);
    }];
    
    _datePicker = [[UIDatePicker alloc] init];
    _datePicker.maximumDate = [NSDate date];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    _datePicker.locale = locale;
    [_datePicker setValue:[UIColor colorWithHexString:@"9eb3c0"] forKey:@"textColor"];
    [bgView addSubview:_datePicker];
    [_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self);
        make.top.mas_equalTo(btnView.mas_bottom).mas_offset(0);
    }];

}

-(void)confirm{
    if (_block) {
        _block(_datePicker.date);
    }
    [self removeFromSuperview];
}

- (void)hidden{
    [self removeFromSuperview];
}

- (void)setMaxDate:(NSDate *)maxDate{
     _datePicker.maximumDate = maxDate;
}

-(void)setMinDate:(NSDate *)minDate{
     _datePicker.minimumDate = minDate;
}

@end
